﻿// ***********************************************************************
// Assembly         : WebChatApi.Common
// Author           : rynn
// Created          : 07-29-2013
//
// Last Modified By : rynn
// Last Modified On : 07-30-2013
// ***********************************************************************
// <copyright file="ResourceCollection.cs" company="">
//     Copyright (c) . All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Resources;
using System.Threading;

/// <summary>
/// The WebService namespace.
/// </summary>
namespace WebChatApi.WebService
{
    /// <summary>
    /// Resource collection for I18N resources
    /// </summary>
    public class ResourceCollection
    {
        /// <summary>
        /// The default code
        /// </summary>
        protected string defaultCode;

        /// <summary>
        /// The language compatibility
        /// </summary>
        protected bool languageCompatibility = false;

        /// <summary>
        /// The resources
        /// </summary>
        protected Dictionary<string, ResourceManager> resources = new Dictionary<string, ResourceManager>(StringComparer.InvariantCultureIgnoreCase);

        /// <summary>
        /// The instance
        /// </summary>
        protected static ResourceCollection instance = new ResourceCollection();

        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>The instance.</value>
        public static ResourceCollection Instance
        {
            get
            {
                return Instance;
            }
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="ResourceCollection" /> class from being created.
        /// </summary>
        protected ResourceCollection()
        {
            this.defaultCode = "en-US";
            AddResourceManagers();
        }

        /// <summary>
        /// Adds the resource managers.
        /// </summary>
        protected virtual void AddResourceManagers()
        {
        }

        /// <summary>
        /// Gets the <see cref="ResourceManager" /> with the specified culure code.
        /// </summary>
        /// <param name="culureCode">The culure code.</param>
        /// <returns>ResourceManager.</returns>
        public ResourceManager this[string culureCode]
        {
            get
            {
                return GetResourceManagerByCode(culureCode, this.languageCompatibility);
            }
        }

        /// <summary>
        /// Gets the resource manager by code.
        /// </summary>
        /// <param name="code">The code.</param>
        /// <param name="languageCompatibility">if set to <c>true</c> [language compatibility].</param>
        /// <returns>ResourceManager.</returns>
        public ResourceManager GetResourceManagerByCode(string code = null, bool languageCompatibility = false)
        {
            if (string.IsNullOrWhiteSpace(code))
            {
                code = defaultCode;
            }

            if (resources.ContainsKey(code))
            {
                return resources[code];
            }
            else if (languageCompatibility && code.Contains('-'))
            {
                string languageCode = code.Split('-')[0];

                if (resources.ContainsKey(languageCode))
                {
                    return resources[languageCode];
                }
            }

            return resources[defaultCode];
        }

        /// <summary>
        /// Gets the resource.
        /// </summary>
        /// <param name="cultureCode">The culture code.</param>
        /// <param name="key">The key.</param>
        /// <returns>System.String.</returns>
        public static string GetResource(string cultureCode, string key)
        {
            return instance[cultureCode].GetString(key);
        }

        /// <summary>
        /// Gets the resource.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns>System.String.</returns>
        public static string GetResource(string key)
        {
            return instance[Thread.CurrentThread.CurrentCulture.Name].GetString(key);
        }


        /// <summary>
        /// Gets the resource manager.
        /// </summary>
        /// <param name="cultureCode">The culture code.</param>
        /// <returns>ResourceManager.</returns>
        public static ResourceManager GetResourceManager(string cultureCode)
        {
            return instance[cultureCode];
        }
    }
}
